/**
 * 学生管理控制器类
 * 功能：接收用户输入指令，协调调用Service层和View层
 * 包含添加、删除、修改、查询学生等功能
 */

public class StudentController {
    // 学生服务对象，处理学生数据逻辑
    private StudentService service = new StudentService();
    // 学生视图对象，处理用户界面显示
    private StudentView view = new StudentView();

    // 启动学生管理控制台程序
    public void start() {
        while (true) {
            view.showMenu();
            String choice = view.getInput();

            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    findStudent();
                    break;
                case "5":
                    view.showMessage("感谢使用，再见！");
                    view.closeScanner();
                    System.exit(0);
                default:
                    view.showMessage("无效输入，请重新选择！");
            }
        }
    }

    // 添加新学生
    private void addStudent() {
        String id = view.inputId();
        if (service.findStudent(id) != null) {
            view.showMessage("该学号已存在！");
            return;
        }

        String name = view.inputName();
        int age = view.inputAge();
        String address = view.inputAddress();

        Student student = new Student(id, name, age, address);
        if (service.addStudent(student)) {
            view.showMessage("学生添加成功！");
        } else {
            view.showMessage("添加失败！");
        }
    }

    // 删除学生
    private void deleteStudent() {
        String id = view.inputId();
        if (service.deleteStudent(id)) {
            view.showMessage("学生删除成功！");
        } else {
            view.showMessage("该学号不存在！");
        }
    }

    // 更新学生信息
    private void updateStudent() {
        String id = view.inputId();
        Student student = service.findStudent(id);
        if (student == null) {
            view.showMessage("该学号不存在！");
            return;
        }

        // 输入新信息
        System.out.print("请输入新姓名（原姓名：" + student.getName() + "）：");
        String newName = view.getInput();

        // 修改后的年龄输入部分
        int newAge;
        while (true) {
            System.out.print("请输入新年龄（原年龄：" + student.getAge() + "）：");
            String newAgeStr = view.getInput();
            try {
                newAge = Integer.parseInt(newAgeStr);
                break;
            } catch (NumberFormatException e) {
                view.showMessage("无效的年龄输入，请重新输入！");
            }
        }

        System.out.print("请输入新住址（原住址：" + student.getAddress() + "）：");
        String newAddress = view.getInput();

        // 检测信息是否发生变化
        if (newName.equals(student.getName()) &&
                newAge == student.getAge() &&
                newAddress.equals(student.getAddress())) {
            view.showMessage("信息未发生变化，无需修改！");
            return;
        }

        if (service.updateStudent(id, newName, newAge, newAddress)) {
            view.showMessage("学生信息修改成功！");
        } else {
            view.showMessage("修改失败！");
        }
    }

    // 查找学生信息
    private void findStudent() {
        System.out.print("\n请输入要查询的学生学号（输入 'all' 查看所有学生）：");
        String id = view.getInput();

        if (id.equalsIgnoreCase("all")) {
            System.out.println("\n所有学生信息：");
            for (Student student : service.getAllStudents()) {
                System.out.println(student);
            }
        } else {
            Student student = service.findStudent(id);
            if (student != null) {
                System.out.println("\n查询结果：");
                System.out.println(student);
            } else {
                System.out.println("该学号不存在！");
            }
        }
    }
}
